﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Ext.Net;
using System.IO;
using Viewer.BusinessObject.BusinessEntities;

namespace DemoExtNet.UserControls
{
    public partial class UCQuyetDinh : System.Web.UI.UserControl
    {
        NSQuyetDinh quyetDinh = new NSQuyetDinh();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadData();
            }
        }
        /// <summary>
        /// Load data combobox theo file xml
        /// </summary>
        private void LoadData()
        {
            string xmlFile = Server.MapPath("~/resources/LoaiQuyetDinh.xml");
            DataSet ds = new DataSet();
            ds.ReadXml(xmlFile);
            storeQuyetDinh.DataSource = ds.Tables["QuyetDinh"];
            storeQuyetDinh.DataBind();
            dtNgayQD.Text = string.Empty;
        }
        [DirectMethod]
        public void CallPreview()
        {
            String filePath = Server.MapPath("~/resources/Document/" + hdFileValue.Text);
            System.IO.FileInfo file = new System.IO.FileInfo(filePath);
            if (file.Exists)
            {
                Response.ClearContent();
                Response.ClearHeaders();
                Response.Cache.SetCacheability(HttpCacheability.Public);
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Length", file.Length.ToString());
                Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                Response.WriteFile(file.FullName);
                Response.Flush();

            }
            else
            {
                X.Msg.Alert("Thông báo", "Bạn chưa chọn file.").Show();
            }
        }
        [DirectMethod]
        public void CheckFile(string title)
        {
            string strFileType = System.IO.Path.GetExtension(flPath.FileName).ToString().ToLower();
            if (strFileType.Equals(".pdf"))
            {

                string strFileName = DateTime.Now.ToString("ddMMyyyy_HHmmss");
                FileStream writeStream = new FileStream(Server.MapPath("~/resources/Document/" + strFileName + strFileType), FileMode.Create, FileAccess.Write);
                ReadWriteStream(flPath.FileContent, writeStream);
                hdFileValue.Text = strFileName + strFileType;
            }
            else
            {
                X.Msg.Alert("Thông báo", "Xin vui lòng chọn file pdf.").Show();
            }

        }
        private void ReadWriteStream(Stream readStream, Stream writeStream)
        {

            int Length = 256;
            Byte[] buffer = new Byte[Length];
            int bytesRead = readStream.Read(buffer, 0, Length);
            while (bytesRead > 0)
            {
                writeStream.Write(buffer, 0, bytesRead);
                bytesRead = readStream.Read(buffer, 0, Length);
            }
            readStream.Close();
            writeStream.Close();
        }
        /// <summary>
        /// Xử lí theo command truyền vào bên form cha
        /// </summary>
        /// <param name="command"></param>
        /// <param name="qdId"></param>
        public void Command(string command, int qdId)
        {

            int loai = Convert.ToInt16(cbbLoaiQuyetDinh.Value);
            switch (command)
            {
                case "Sua":
                    quyetDinh.Edit(qdId, loai, dtNgayQD.SelectedDate, flPath.FileName, txtGhiChu.Text, txtMaQD.Text);
                    break;
                case "Xoa":
                    quyetDinh.DeleteQuyetDinh(qdId);
                    break;
                case "LoadByID":
                    GetQuyetDinhById(qdId);
                    break;
                default:
                    break;
            }
        }
        /// <summary>
        /// load quyết định theo id
        /// </summary>
        /// <param name="qdID"></param>
        private void GetQuyetDinhById(int qdID)
        {
            NSQuyetDinh item = quyetDinh.GetQuyetDinhByID(qdID);
            cbbLoaiQuyetDinh.Value = item.LoaiQuyetDinh;
            txtGhiChu.Text = item.GhiChu;
            txtMaQD.Text = item.MaQD;
            flPath.Text = item.TitleFile;
            hdFileValue.Value = item.PathFileQD;
            dtNgayQD.Text = item.NgayQD.HasValue ? item.NgayQD.Value.ToString("dd/MM/yyyy") : string.Empty;
        }
        public void ResetControl(string page)
        {
            txtGhiChu.Text = string.Empty;
            txtMaQD.Text = string.Empty;
            flPath.Text = string.Empty;
            cbbLoaiQuyetDinh.Value = page;
            dtNgayQD.Text = string.Empty;
        }
        public int TaoMoiQuyetDinh()
        {
            int loai = Convert.ToInt16(cbbLoaiQuyetDinh.SelectedItem.Value);
            string fileUpload = hdFileValue.Text;

            return quyetDinh.InsertQuyetDinh(loai, dtNgayQD.SelectedDate, txtGhiChu.Text, txtMaQD.Text, fileUpload, flPath.FileName);
        }
    }
}